import {
	defineStore
} from 'pinia'
import {
	getClassifyCascadeData,
	getClassifyRecord,
	getMistakeQuestionList,
	getMockQuestionList,
	getOrderQuestionList,
	getOutOrderQuestionList
} from '@/service/modules/base-question'
import {
	useTestQuestionStore
} from './test-question'

export const useBaseQuestionStore = defineStore('base-question', {
	state: () => ({
		// 级联分类数据
		cascadeList: [],
		// 题目对应的统计信息
		classifyRecord: {},
		// 当前题库的 类型classifyId和级别数type
		questionParams: {},
		// 打开自定义导航栏的选择框的方法
		openCascadeSelect: () => {},
		// 类型 0 顺序刷题 1逆序刷题 2错题集训 3考试记录
		flag: -1,
		// 是否选择了题库类型还要跳转答题页
		isNeedSelect: false,
		closeTimes: 0,
		// 题库
		// questionList: [],
	}),
	actions: {
		// 获取基础面试题页面左上角级联数据
		async getClassifyCascadeDataAction() {
			const res = await getClassifyCascadeData()
			this.cascadeList = res.data
		},
		// 获取题目对应的统计信息
		async getClassifyRecordAction(classifyId, type) {
			const res = await getClassifyRecord(classifyId, type)
			this.classifyRecord = res.data
		},
		// 保存当前题库的 类型id和级别数type
		async saveQuestionParamsAction(classifyId, type) {
			this.questionParams = {
				classifyId,
				type
			}
		},
		// 保存一个方法可以打开自定义导航栏的选择框
		openCascadeSelectAction(fn) {
			this.openCascadeSelect = fn
		},
		// 判断是否选中了题库类型
		isSelectQuestionTypeAction() {
			if (Object.keys(this.questionParams).length === 0) {
				uni.showToast({
					icon: "error",
					title: "请先选择类型"
				})
				// 重置状态
				this.isNeedSelect = true
				this.closeTimes = 0
				// 手动打开选择题型的框
				this.openCascadeSelect()
				return false
			}
			return true
		},
		// 手动选择了题库类型才进入答题页面的情况
		afterSelect() {
			this.getQuestionListAction(this.flag)
			// 只要请求成功证明不再需要选择题库类型，将状态置为false
			this.isNeedSelect = false
			this.closeTimes = 0
		},
		// 获取答题的题目数据
		async getQuestionListAction(mode) {
			console.log(mode);
			// 更新当前刷题模式
			this.flag = mode
			// 判断是否选择了类型，只有选择了类型才能进入答题页面
			if (!this.isSelectQuestionTypeAction()) return
			// 真正请求数据
			const {
				classifyId,
				type
			} = this.questionParams
			// 根据模式请求对应的数据
			let getList
			let title
			switch (mode) {
				// 类型 0 顺序刷题 1乱刷题 2错题集训 3考试记录
				case 0:
					getList = getOrderQuestionList
					title = '顺序答题'
					break;
				case 1:
					getList = getOutOrderQuestionList
					title = '乱序答题'
					break;
				case 2:
					getList = getMistakeQuestionList
					title = '错题集训'
					break;
				case 3:
					getList = getMockQuestionList
					title = '模拟考试'
					break;
				default:
					break;
			}
			const res = await getList(classifyId, type)
			// 重置状态
			const testQuestionStore = useTestQuestionStore()
			// console.log(type, 111);
			testQuestionStore.isReviewing = false
			if (mode === 1 && res.data.length >= 3) {
				const a1 = res.data[2]
				const a2 = res.data[0]
				res.data[0] = a1
				res.data[2] = a2
				testQuestionStore.questionList = res.data
			} else {
				console.log(res.data);
				testQuestionStore.questionList = res.data
			}
			// 跳转答题页面
			uni.navigateTo({
				url: '/pages/test-question/test-question?title=' + encodeURIComponent(title)
			})
			console.log(res);
		},
	},
})